package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-22
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    // 添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        // 查询所有一级分类
        QueryWrapper<EduSubject> queryWrapperOne = new QueryWrapper<>();
        queryWrapperOne.eq("parent_id", 0);
        List<EduSubject> OneeduSubjects = baseMapper.selectList(queryWrapperOne);
        // 查询所有二级分类
        QueryWrapper<EduSubject> queryWrapperTwo = new QueryWrapper<>();
        queryWrapperOne.ne("parent_id", 0);
        List<EduSubject> TwoeduSubjects = baseMapper.selectList(queryWrapperTwo);
        // 创建list集合，用于储存最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        // 封装一级分类
           /* 将查询出来的所有一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值
             封装到finalSubjectList中*/
        for (int i = 0; i < OneeduSubjects.size(); i ++ ) {
            OneSubject oneSubject = new OneSubject();
            EduSubject eduSubject = OneeduSubjects.get(i);
            oneSubject.setId(eduSubject.getId());
            oneSubject.setTitle(eduSubject.getTitle());

            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (int j = 0; j < TwoeduSubjects.size(); j ++ ) {
                EduSubject twoSubject = TwoeduSubjects.get(j);
                if (twoSubject.getParentId().equals(eduSubject.getId())) {
                    TwoSubject twoSubject1 = new TwoSubject();
                    BeanUtils.copyProperties(twoSubject, twoSubject1);
                    twoFinalSubjectList.add(twoSubject1);
                }
            }

            oneSubject.setChildren(twoFinalSubjectList);
            finalSubjectList.add(oneSubject);
        }

        return finalSubjectList;
    }
}
